package bc.swim2.web.servlet;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bc.swim2.ejb.entity.Admin;
import bc.swim2.ejb.entity.RegisteredUser;
import bc.swim2.ejb.session.AdminManagerBeanRemote;
import bc.swim2.ejb.session.UserManagerBeanRemote;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private Context ctx;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		if (request.getParameter("methodName").equals("logout")) {
			logout(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		login(request, response);
	}

	private void login(HttpServletRequest request, HttpServletResponse response) {
		try {
			ctx = new InitialContext();
			Admin admin;
			Object ref2 = ctx.lookup("AdminManagerBean/remote");
			AdminManagerBeanRemote adminManager = (AdminManagerBeanRemote) ref2;
			admin = adminManager.validateLogin(
					request.getParameter("username"),
					request.getParameter("password"));

			if (admin != null) {
				HttpSession session = request.getSession(true);
				session.setAttribute("currentSessionUser", admin);
				session.setAttribute("ctx", ctx);

				response.sendRedirect("MainServlet?to=AdminHome");
			} else {

				try {
					ctx = new InitialContext();
					RegisteredUser user;

					Object ref3 = ctx.lookup("UserManagerBean/remote");

					UserManagerBeanRemote usermanager = (UserManagerBeanRemote) ref3;

					user = usermanager.validateLogin(
							request.getParameter("username"),
							request.getParameter("password"));

					if (user != null) {
						HttpSession session = request.getSession(true);
						session.setAttribute("ctx", ctx);
						session.setAttribute("currentSessionUser", user);
						response.sendRedirect("MainServlet?to=RegisteredUserHome");
					} else {
						request.setAttribute("loginFailed", true);
						request.getRequestDispatcher("index.jsp").forward(
								request, response);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void logout(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			request.getSession().invalidate();
			request.getRequestDispatcher("index.jsp")
					.forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}